<!--
 * @Author: wangzm
 * @Date: 2020-02-18 10:44:23
 * @LastEditors: wjk
 * @LastEditTime: 2020-12-14 19:56:57
 * @Description: share page
 -->
<template>
  <view></view>
</template>
<script>
import { getQueryString, updateQueryStringParameter } from '@/utils/tools'
import { mapGetters, mapActions } from 'vuex'
export default {
  onLoad(option) {
    const shareId = this.$queryPageParam(option, 'id')
    const share_code = this.$queryPageParam(option, 'share_code')
    if (share_code) {
      uni.setStorageSync('share_code', share_code)
    }
    // 扫码进来的
    if (shareId) {
      this.getPathInfo(shareId)
      return
    }

    const path = decodeURIComponent(decodeURIComponent(this.$queryPageParam(option, 'path')))
    console.log('path', path)
    if (!this.login(path)) { return }
    this.goNext(path)
  },
  computed: {
    ...mapGetters(['userToken'])
  },
  methods: {
    async getPathInfo(shareId) {
      const res = await this.$api.commonApi.getMiniCodeParams({ shareCodeId: shareId })
      console.log('getPathInfo', res)
      if (res.success && res.data && res.data.scene) {
        if (!this.login(res.data.scene)) { return }
        this.goNext(decodeURIComponent(res.data.scene))
      } else {
        uni.showToast({
          title: '小程序码已失效',
          icon: 'none',
          success: () => {
            setTimeout(() => {
              this.goHome()
            }, 2000)
          }
        })
      }
    },
    goNext(path) {
      const share_code = getQueryString('share_code', path)
      let newPath = path
      if (path.match('/pagesDetail/commodity/index')) {
        newPath = updateQueryStringParameter(path, 'invite_code', share_code)
      }
      if (path.match('/pages/home/index')) {
        this.$sensorTrack('ENTERPAGE', {
          page_name: 'BXYP_main'
        })
      }
      if (share_code) {
        uni.setStorageSync('share_code', share_code)
      }
      if (newPath) {
        // #ifdef MP-WEIXIN
        uni.reLaunch({
          url: newPath
        })
        // #endif

        // #ifdef H5
        location.replace(location.origin + newPath)
        // #endif
      } else {
        this.goHome()
      }
    },
    login(path) {
      const share_code = getQueryString('share_code', path)
      const user_token = getQueryString('user_token', path)
      // 链接有token就存起来，表示登陆
      if (user_token) {
        this.SET_TOKEN(user_token)
        this.GetUserInfo()
      }
      if (share_code) {
        uni.setStorageSync('share_code', share_code)
      }
      const isNeedLogin = getQueryString('isNeedLogin', path)
      if (isNeedLogin === 1 || isNeedLogin === '1') {
        if (!this.userToken) {
          if (path) {
            uni.setStorageSync('pageAfterLogin', path)
          }
          this.$navigateTo('/pages/login/index', {}, 'redirectTo')
          return false
        }
      }
      return true
    },
    goHome() {
      // #ifdef MP-WEIXIN
      uni.reLaunch({
        url: '/pages/home/index'
      })
      // #endif
      // #ifdef H5
      location.replace(location.origin)
      // #endif
    },
    ...mapActions(['SET_TOKEN', 'GetUserInfo', 'LOGOUT_USER'])
  }
}
</script>
